package com.qd.hik.service;

import cn.hutool.http.HttpResponse;
import com.cdqidi.httpclientbootstart.util.HttpUtil;
import com.cdqidi.util.JacksonUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qd.hik.base.HkBaseTokenDTO;
import com.qd.hik.dto.HkBaseHttpResp;
import com.qd.hik.dto.bag.*;
import com.qd.hik.token.HkAccessToken;
import com.qd.hik.token.HkAccessTokenManager;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 包月
 *
 * @author sjk
 * @create 2024-05-29 16:22
 * @since 1.0.0
 */
@Service
@Slf4j
@RequiredArgsConstructor
@HkAccessToken
public class HkBagService {
    private final HkBaseTokenDTO hkBaseTokenDTO;
    private final HttpUtil httpUtil;
    private final ObjectMapper objectMapper;

    /**
     * 2.6.1 新办理包月套餐
     */
    private static final String BAG_RULES_URL = "/artemis/api/v1/bag/rules";
    /**
     * 2.6.2 包月证充值包月套餐
     */
    private static final String BAG_CERT_RULE_URL = "/artemis/api/v1/bag/certRule";
    /**
     * 2.6.3 新办理包月支付下单
     */
    private static final String BAG_ORDER_URL = "/artemis/api/v1/bag/order";
    /**
     * 2.6.4 包期证充值支付下单
     */
    private static final String BAG_RENEW_BAG_URL = "/artemis/api/v1/bag/renewbag";
    /**
     * 2.6.5 包月充值通知
     */
    private static final String BAG_RENEW_NOTICE_URL = "/artemis/api/v1/bag/renewNotice";
    /**
     * 2.6.6 包月办理通知
     */
    private static final String BAG_ORDER_NOTICE_URL = "/artemis/api/v1/bag/orderNotice";
    /**
     * 2.6.7 包月订单业务办理状态
     */
    private static final String BAG_ORDER_STATE_URL = "/artemis/api/v1/bag/orderState";
    /**
     * 2.6.8 包月车取消预览
     */
    private static final String BAG_PREVIEW_CANCEL_URL = "/artemis/api/v1/bag/previewCancel";
    /**
     * 2.6.9 提交取消包月申请
     */
    private static final String BAG_CANCEL_URL = "/artemis/api/v1/bag/cancel";
    /**
     * 2.6.10 获取包月车记录
     */
    private static final String BAG_RECORDS_URL = "/artemis/api/v1/bag/records";

    /**
     * 2.6.1 新办理包月套餐
     * @param req 包月套餐
     */
    @SneakyThrows
    public HkBaseHttpResp<HkCertRuleResp> rules(HkRuleReq req) {
        String json = JacksonUtil.getObjectMapper().writeValueAsString(req);
        String post = httpUtil.sendHttpsPost(hkBaseTokenDTO.getBaseUrl() + BAG_RULES_URL, json, Map.of("access_token", HkAccessTokenManager.getToken().getAccessToken()));
        return objectMapper.readValue(post, new TypeReference<>() {
        });
    }

    /**
     * 2.6.2 包月证充值包月套餐
     * @param req 包月套餐
     */
    @SneakyThrows
    public HkBaseHttpResp<HkCertRuleResp> certRule(HkCertRuleReq req) {
        String json = JacksonUtil.getObjectMapper().writeValueAsString(req);
        String post = httpUtil.sendHttpsPost(hkBaseTokenDTO.getBaseUrl() + BAG_CERT_RULE_URL, json, Map.of("access_token", HkAccessTokenManager.getToken().getAccessToken()));
        return objectMapper.readValue(post, new TypeReference<>() {
        });
    }

    /**
     * 2.6.3 新办理包月支付下单
     * @param req 包月套餐
     */
    @SneakyThrows
    public HkBaseHttpResp<HkOrderResp> order(HkOrderReq req) {
        String json = JacksonUtil.getObjectMapper().writeValueAsString(req);
        String post = httpUtil.sendHttpsPost(hkBaseTokenDTO.getBaseUrl() + BAG_ORDER_URL, json, Map.of("access_token", HkAccessTokenManager.getToken().getAccessToken()));
        return objectMapper.readValue(post, new TypeReference<>() {
        });
    }

    /**
     * 2.6.4 包期证充值支付下单
     *
     * @param req 包月套餐
     */
    @SneakyThrows
    public HkBaseHttpResp<HkOrderResp> renewBag(HkRenewBagReq req) {
        String json = JacksonUtil.getObjectMapper().writeValueAsString(req);
        String post = httpUtil.sendHttpsPost(hkBaseTokenDTO.getBaseUrl() + BAG_RENEW_BAG_URL, json, Map.of("access_token", HkAccessTokenManager.getToken().getAccessToken()));
        return objectMapper.readValue(post, new TypeReference<>() {
        });
    }

    /**
     * 2.6.5 包月充值通知
     *
     * @param req 包月套餐
     * @return 成功 or 失败
     */
    @SneakyThrows
    public HkBaseHttpResp<?> renewNotice(HkRenewBagReq req) {
        String json = JacksonUtil.getObjectMapper().writeValueAsString(req);
        String post = httpUtil.sendHttpsPost(hkBaseTokenDTO.getBaseUrl() + BAG_RENEW_NOTICE_URL, json, Map.of("access_token", HkAccessTokenManager.getToken().getAccessToken()));
        return objectMapper.readValue(post, HkBaseHttpResp.class);
    }

    /**
     * 2.6.6 包月办理通知
     *
     * @param req 包月套餐
     * @return 成功 or 失败
     */
    @SneakyThrows
    public HkBaseHttpResp<HkOrderNoticeResp> orderNotice(HkOrderNoticeReq req) {
        String json = JacksonUtil.getObjectMapper().writeValueAsString(req);
        String post = httpUtil.sendHttpsPost(hkBaseTokenDTO.getBaseUrl() + BAG_ORDER_NOTICE_URL, json, Map.of("access_token", HkAccessTokenManager.getToken().getAccessToken()));
        return objectMapper.readValue(post, new TypeReference<>() {
        });
    }

    /**
     * 2.6.7 包月订单业务办理状态
     *
     * @param orderNo 账单编号
     * @return 成功 or 失败
     */
    @SneakyThrows
    public HkBaseHttpResp<HkOrderStateResp> orderState(String orderNo) {
        try (HttpResponse httpResponse = cn.hutool.http.HttpUtil.createGet(hkBaseTokenDTO.getBaseUrl() + BAG_ORDER_STATE_URL + "?orderNo=" + orderNo).header("access_token", HkAccessTokenManager.getToken().getAccessToken()).execute()) {
            return objectMapper.readValue(httpResponse.body(), new TypeReference<>() {
            });
        }
    }

    /**
     * 2.6.8 包月车取消预览
     *
     * @param certCode 凭证编号
     * @return 成功 or 失败
     */
    @SneakyThrows
    public HkBaseHttpResp<HkPreviewCancelResp> previewCancel(String certCode) {
        try (HttpResponse httpResponse = cn.hutool.http.HttpUtil.createGet(hkBaseTokenDTO.getBaseUrl() + BAG_PREVIEW_CANCEL_URL + "?certCode=" + certCode).header("access_token", HkAccessTokenManager.getToken().getAccessToken()).execute()) {
            return objectMapper.readValue(httpResponse.body(), new TypeReference<>() {
            });
        }
    }

    /**
     * 2.6.9 提交取消包月申请
     *
     * @param certCode   凭证编号
     * @param cancelTime 取消预览的时间戳
     * @return 成功 or 失败
     */
    @SneakyThrows
    public HkBaseHttpResp<?> cancel(String certCode, String cancelTime) {
        String json = objectMapper.createObjectNode().put("certCode", certCode).put("cancelTime", cancelTime).toString();
        String post = httpUtil.sendHttpsPost(hkBaseTokenDTO.getBaseUrl() + BAG_CANCEL_URL, json, Map.of("access_token", HkAccessTokenManager.getToken().getAccessToken()));
        return objectMapper.readValue(post, HkBaseHttpResp.class);
    }

    /**
     * 2.6.10 获取包月车记录
     *
     * @param driverId 驾驶人编号
     * @return 成功 or 失败
     */
    @SneakyThrows
    public HkBaseHttpResp<HkRecordsResp> records(Integer driverId) {
        try (HttpResponse httpResponse = cn.hutool.http.HttpUtil.createGet(hkBaseTokenDTO.getBaseUrl() + BAG_RECORDS_URL + "?driverId=" + driverId).header("access_token", HkAccessTokenManager.getToken().getAccessToken()).execute()) {
            return objectMapper.readValue(httpResponse.body(), new TypeReference<>() {
            });
        }
    }
}